<template>
	<u-popup :show="show" @close="close" @open="open" mode="bottom" :round="12" @touchmove.stop.prevent="">
		<view class="custom_share">
			<button class="custom_share_item" open-type="share">
				<image src="http://qianxueapplet.suoeryun.com/forwardFriends.png"></image>
				<text>发送给微信好友</text>
			</button>
			<view class="custom_share_item" @click="handlePoster">
				<image src="http://qianxueapplet.suoeryun.com/generate.png"></image>
				<text>生成分享海报</text>
			</view>
		</view>
	</u-popup>
	<!-- 海报 -->
	<customPoster ref="customPosterRef" @success="show = false" />
</template>

<script setup lang="ts">
	import { ref, unref } from 'vue';
	import { onShareAppMessage } from "@dcloudio/uni-app";
	import { updateActivityNum, addActivityMediaLikesAndBrowse } from '@/config/index.api';
	import { transmitDynamics } from '@/config/circle.api';
	import customPoster from '@/components/customPoster.vue';

	const show = ref(false);
	const params = ref();
	const customPosterRef = ref();

	/**
	 * 分享
	 */
	onShareAppMessage(async (res) => {
		const item = unref(params);
		console.log(item, 'sdf');
		if (item?.path === 'pages/index/acitvityDetails') {
			await updateActivityNum({ activityId: item?.activityId, operationType: '2' });
		} else if (item?.path === 'pages/circle/circleDetails') {
			await transmitDynamics({ id: item?.activityId });
		} else {
			await addActivityMediaLikesAndBrowse({ activityId: item?.activityId, operationType: '2' });
		}
		return {
			title: item?.title ?? '分享',
			path: `${item?.path}?${item?.scene}`
		}
	})

	async function handleShow(item) {
		show.value = true;
		params.value = item;
	}

	/**
	 * 海报
	 */
	async function handlePoster() {
		const item = unref(params);
		console.log(item?.path, 'item?.path');
		if (item?.path === 'pages/index/acitvityDetails') {
			await updateActivityNum({ activityId: item?.activityId, operationType: '2' });
		} else if (item?.path === 'pages/circle/circleDetails') {
			await transmitDynamics({ id: item?.activityId });
		} else {
			await addActivityMediaLikesAndBrowse({ activityId: item?.activityId, operationType: '2' });
		}
		customPosterRef.value.handleShow(item)
	}

	/**
	 * 打开
	 */
	function open() {
		console.log('open');
	}

	/**
	 * 关闭
	 */
	function close() {
		show.value = false;
		console.log('close');
	}

	defineExpose({
		handleShow,
	})
</script>

<style lang="scss">
	.custom_share {
		display: flex;
		align-items: center;
		justify-content: space-around;
		padding: 44rpx 0;

		.custom_share_item {
			display: flex;
			flex-direction: column;
			align-items: center;
			margin: 0;
			padding: 0;
			background-color: transparent;

			&::after {
				border: 0;
			}

			image {
				width: 144rpx;
				height: 144rpx;
			}

			text {
				font-size: 28rpx;
				color: 000000;
				line-height: 48rpx;
			}
		}
	}
</style>